package com.lvy.hczn.transmit.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.common.annotation.Log;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.core.domain.entity.SysUser;
import com.lvy.hczn.front.common.core.page.TableDataInfo;
import com.lvy.hczn.front.common.enums.BusinessType;
import com.lvy.hczn.front.common.utils.poi.ExcelUtil;
import com.lvy.hczn.front.system.domain.business.GoodsBox;
import com.lvy.hczn.front.system.domain.system.SupplierBoxPrefix;
import com.lvy.hczn.front.system.domain.transmit.SupplierBoxBatchNumber;
import com.lvy.hczn.front.system.domain.transmit.dto.SupplierBoxBatchNumberExport;
import com.lvy.hczn.front.system.service.SupplierBoxBatchNumberService;
import com.lvy.hczn.front.system.service.SupplierBoxPrefixService;
import com.lvy.hczn.transmit.service.GoodsBoxService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequestMapping("/system/box")
@Controller
@Slf4j
public class GoodsBoxController extends BaseController {

    @Autowired
    private GoodsBoxService goodsBoxService;

    private String prefix = "system/box";

    @RequiresPermissions("system:box:view")
    @GetMapping()
    public String dictData() {
        return prefix + "/box";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:box:list")
    @ResponseBody
    public TableDataInfo list(GoodsBox goodsBox) {
        startPage();
        List<GoodsBox> list = goodsBoxService.selectBoxlist(goodsBox);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @RequiresPermissions("system:box:add")
    @Log(title = "货箱添加", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsBox goodsBox) {
        return toAjax(goodsBoxService.save(goodsBox));
    }

    @RequiresPermissions("system:box:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GoodsBox goodsBox = goodsBoxService.getById(id);
        return prefix + "/edit";
    }

    @RequiresPermissions("system:box:edit")
    @Log(title = "货箱编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsBox goodsBox) {
        return toAjax(goodsBoxService.updateById(goodsBox));
    }

    @RequiresPermissions("system:box:remove")
    @Log(title = "货箱删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodsBoxService.deleteGoodsBoxByIds(ids));
    }

    @Log(title = "货箱管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:box:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<GoodsBox> util = new ExcelUtil<GoodsBox>(GoodsBox.class);
        List<GoodsBox> userList = util.importExcel(file.getInputStream());
        String message = goodsBoxService.importGoodsBox(userList, updateSupport, getLoginName(), getSysUser(),true);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("system:box:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<GoodsBox> util = new ExcelUtil<GoodsBox>(GoodsBox.class);
        return util.importTemplateExcel("货箱数据");
    }
}
